import Vue from 'vue'
import VueRouter from 'vue-router'
import notFound from "@/views/404.vue"
import Main from "@/views/main.vue"
import { getStorageUserInfo } from '@/utils'
Vue.use(VueRouter)
import "@/assets/common/commonStyle.scss"
import {UserModule} from "@/store/modules/userModules";

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
    if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
    return originalPush.call(this, location).catch(err => err)
}
const publicRoutes = ['/login']
const routes = [
    {
        path: '/',
        name: 'homeParent',
        component: Main,
        redirect: '/home',
        meta: { permissions: [], title: '主页' },
        children: [
            {
                path: '/home',
                name: 'home',
                component: () => import('../views/home.vue'),
                meta: { permissions: [], title: '主页' },
            }
        ]
    },

    {
        path: '/login',
        name: 'login',
        component: () => import('../views/login.vue'),
    },
    {
        path: '/404',
        name: 'Page404',
        component: notFound
    },
    {
        path: '*',
        redirect: '/404'
    },
]

export const operatorRoutes = [
    {
        path: '/smartWarning',
        name: 'smartWarningParent',
        component: Main,
        icon: 'appstore',
        meta: { permissions: [], title: '智能预警' },
        children: [
            {
                path: '/smartWarning',
                name: 'smartWarning',
                component: () => import('../views/smartWarningView/index.vue'),
                meta: { permissions: ['medicinalPlaceOfOrigin'], title: '智能预警' },
            },
        ],
    },
    {
        path: '/productPlan',
        name: 'productPlan',
        component: Main,
        icon: 'file-text',
        meta: { permissions: ['processPlan'], title: '生产计划' },
        children: [
            // {
            //     path: '/productionPlan',
            //     name: 'productionPlan',
            //     component: () => import('../views/productPlanView/productionPlan/index.vue'),
            //     meta: { permissions: ['productionPlan'], title: '生产计划' },
            // },
            {
                path: '/packagingPlan',
                name: 'packagingPlan',
                component: () => import('../views/productPlanView/packagingPlan/index.vue'),
                meta: { permissions: ['packPlan'], title: ' 包装计划' },
            },
            {
                path: '/drugPlan',
                name: 'drugPlan',
                component: () => import('../views/productPlanView/drugPlan/index.vue'),
                meta: { permissions: ['preparationPlan'], title: '制剂计划' },
            },
            {
                path: '/extractPlan',
                name: 'extractPlan',
                component: () => import('../views/productPlanView/extractPlan/index.vue'),
                meta: { permissions: ['extractPlan'], title: '提取计划' },
            },
            {
                path: '/pillsPlan',
                name: 'pillsPlan',
                component: () => import('../views/productPlanView/pillsPlan/index.vue'),
                meta: { permissions: ['piecesPlan'], title: ' 饮片计划' },
            },
        ],
    },
    {
        path: '/productSchedule',
        name: 'productSchedule',
        component: Main,
        icon: 'snippets',
        children: [
            {
                path: '/productScheduleIndex',
                name: 'productScheduleIndex',
                component: () => import('../views/productSchedule/index.vue'),
                meta: { permissions: ['productionSchedule'], title: '生产进度' },
            },
            {
                path: '/productScheduleEchart',
                name: 'productScheduleEchart',
                component: () => import('../views/productSchedule/indexTwo.vue'),
                meta: { permissions: ['productionSchedule'], title: '生产统计' },
            },
        ],
        meta: { permissions: [], title: '生产进度' },
    },
    {
        path: '/procurementPlan',
        name: 'procurementPlanParent',
        component: Main,
        icon: 'account-book',
        children: [
            {
                path: '/procurementPlan',
                name: 'procurementPlan',
                component: () => import('../views/procurementPlanView/index.vue'),
                meta: { permissions: ['purchasePlan'], title: '采购计划' },
            },
        ],
        meta: { permissions: [], title: '采购计划' },
    },
    {
        path: '/inspection',
        name: 'inspection',
        component: Main,
        icon: 'pull-request',
        children: [
            {
                path: '/inspection',
                name: 'inspection',
                component: () => import('../views/inspectionView/index.vue'),
                meta: { permissions: ['checkoutDetection'], title: '检验检测' },
            },
        ],
        meta: { permissions: [], title: '检验检测' },
    },
    {
        path: '/setting',
        name: 'setting',
        component: Main,
        icon: 'setting',
        meta: { permissions: ['system'], title: '系统设置' },
        children: [
            {
                path: '/materialMgt',
                name: 'materialMgt',
                component: () => import('../views/settingView/materialMgt/index.vue'),
                meta: { permissions: ['materialManagement'], title: '物料管理' },
            },
            {
                path: '/productMgt',
                name: 'productMgt',
                component: () => import('../views/settingView/productMgt/index.vue'),
                meta: { permissions: ['productManagement'], title: '产品管理' },
            },
            {
                path: '/productionMgt',
                name: 'productionMgt',
                component: () => import('../views/settingView/productionMgt/index.vue'),
                meta: { permissions: ['productionEquipment'], title: '生产设备' },
            },
            {
                path: '/staffMgt',
                name: 'staffMgt',
                component: () => import('../views/settingView/staffMgt/index.vue'),
                meta: { permissions: ['personnelManagement'], title: '人员管理' },
            },
            {
                path: '/permissionMgt',
                name: 'permissionMgt',
                component: () => import('../views/settingView/permissionMgt/index.vue'),
                meta: { permissions: ['authorityManagement'], title: '权限管理' },
            },
        ],
    },
    {
        path: '/report',
        name: 'report',
        component: Main,
        icon: 'file-search',
        meta: { permissions: ['report'], title: '报表管理' },
        children: [
            {
                path: '/dailyReport',
                name: 'dailyReport',
                component: () => import('@/views/reportMgt/productionDailyReport/dailyReport.vue'),
                meta: { permissions: ['productionStatement'], title: '生产日报表' },
            },
            {
                path: '/transInventoryTable',
                name: 'transInventoryTable',
                component: () => import('@/views/reportMgt/transitInventory/index.vue'),
                meta: { permissions: ['entrepotStorageStatement'], title: '中转库成品库存表' },
            },
            {
                path: '/rawMaterialInventoryTable',
                name: 'rawMaterialInventoryTable',
                component: () => import('@/views/reportMgt/rawMaterialInventory/index.vue'),
                meta: { permissions: ['medicinalStatement'], title: '原药材库存表' },
            },
            // {
            //     path: '/pillsInventoryTable',
            //     name: 'pillsInventoryTable',
            //     component: () => import('../views/reportMgt/pillsInventory/index.vue'),
            //     meta: { permissions: ['piecesStatement'], title: '提取饮片库存表' },
            // },
            // {
            //     path: '/grainInventoryTable',
            //     name: 'grainInventoryTable',
            //     component: () => import('@/views/reportMgt/grainInventory/index.vue'),
            //     meta: { permissions: ['extractPowderStatement'], title: '颗粒提取物库存表' },
            // },
            // {
            //     path:'/powderJournalTable',
            //     name:'powderJournalTable',
            //     component:()=>import('@/views/reportMgt/powderJournal/index.vue'),
            //     meta:{permissions:['extractPowderStatement'],title:'提取物流水账表'},
            // },
            {
                path:'/halfRawProductTable',
                name:'halfRawProductTable',
                component: () =>import('@/views/reportMgt/halfRawProduct/halfRawProductTable.vue'),
                meta:{permissions:['nonProd'],title:'半成品库存表'},
            },
            {
                path:'/productInventory',
                name:'productInventory',
                component: () =>import('@/views/reportMgt/productInventory/index.vue'),
                meta:{permissions:['prodStock'],title:'成品库存表'},
            },
            {
                path:'/productSendStatistics',
                name:'productSendStatistics',
                component: () =>import('@/views/reportMgt/productSendStatistics/index.vue'),
                meta:{permissions:['goods'],title:'发货量统计表'},
            },
            {
                path:'/materialBalanceStatistics',
                name:'materialBalanceStatistics',
                component: () =>import('@/views/reportMgt/materialBalanceStatistics/index.vue'),
                meta:{permissions:['balance'],title:'物料平衡统计表'},
            },
            {
                path:'/productionProgress',
                name:'productionProgress',
                component: () =>import('@/views/reportMgt/productionProgress/index.vue'),
                meta:{permissions:['balance'],title:'生产跟进表'},
            },
        ],

    },

]


const router = new VueRouter({
    mode: 'hash',
    base: process.env.BASE_URL,
    routes: routes
})

router.beforeEach((to, from, next) => {
    let userInfo = getStorageUserInfo()
    if (userInfo && userInfo.id) {
        UserModule.setMenuChange(to.path)
        //已登录
        next()
        //设置菜单栏选择状态
    } else if (publicRoutes.indexOf(to.path) !== -1) {
        //公开路由
        next()
    } else {
        //
        next('/login')
    }

});

router.afterEach(route => {
    //
});
export const setRouter = (callback) => {
    const list = generateRoutes()
    for (let r of list) {
        router.addRoute(r)
    }
    callback()
}
const hasPermission = (permissions, route) => {
    if (route.meta && route.meta.permissions && route.meta.permissions.length > 0) {
        return permissions.some(permission => route.meta.permissions.includes(permission))
    } else {
        return true
    }
}
const filterAsyncRoutesWithPermissions = (routes, permissions) => {
    const res = []
    routes.forEach(route => {
        const r = { ...route }
        if (hasPermission(permissions, r)) {
            if (r.children) {
                r.children = filterAsyncRoutesWithPermissions(r.children, permissions)
                if(r.children.length>0) res.push(r)
            }else{
                res.push(r)
            }

        }
    })
    return res
}
export const generateRoutes = () => {
    const userInfo = getStorageUserInfo()

    if (userInfo && userInfo.id) {
        const permissions = userInfo.resourceChildren.map(item => item.name)
        return filterAsyncRoutesWithPermissions(operatorRoutes, permissions)
    }
    return []
}
export default router
